package cn.shop.gateway.http;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.shop.gateway.db.DbUtil;
import cn.shop.gateway.model.GameOrder;
import cn.shop.gateway.model.MerchantGameTemplate;
import cn.shop.gateway.model.MerchantUser;
import cn.shop.gateway.utils.JsonUtil;
import cn.shop.gateway.utils.ResponseInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 李一涵
 * @date : Created in 2021-12-10 14:51
 * @description :
 */
public class ApiUtil {

    private static final Logger LOG = LoggerFactory.getLogger(ApiUtil.class);

    private static String HOST = "http://192.168.1.9";
    private static Integer PORT = 83;
    private final static String BASE_PATH = "/game/api";
    private final static String AUTHENTICATION_URL = "/login";
    private final static String GET_TEMPLATE_LIST = "/{}/template/all";
    private final static String SEND_ORDER = "/{}/order/add";

    public static MerchantUser auth(String account, String password) {
        String secret = DbUtil.getSecret();
        Map<String, Object> form = new HashMap<>(4);
        form.put("account", account);
        form.put("password", password);
        String json = HttpUtil.createPost(formatUrl(AUTHENTICATION_URL))
                .header("secret", secret)
                .form(form)
                .execute()
                .body();
        LOG.info("auth 返回结果 {}", json);
        if (StrUtil.isBlank(json)) {
            return null;
        } else {
            ResponseInfo<MerchantUser> result = null;
            try {
                result = JsonUtil.mapper().readValue(json,
                        new TypeReference<ResponseInfo<MerchantUser>>() {});
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return result != null ? result.getData() : null;
        }
    }

    public static List<MerchantGameTemplate> getTemplates(long id)  {
        String secret = DbUtil.getSecret();
        String json = HttpUtil.createGet(formatUrl(StrUtil.format(GET_TEMPLATE_LIST, id)))
                .header("secret", secret)
                .execute()
                .body();
        LOG.info("secret {} templates 返回结果 {}", secret, json);
        if (StrUtil.isBlank(json)) {
            return null;
        } else {
            ResponseInfo<List<MerchantGameTemplate>> result = null;
            try {
                result = JsonUtil.mapper().readValue(json,
                        new TypeReference<ResponseInfo<List<MerchantGameTemplate>>>() {});
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return result != null ? result.getData() : Collections.emptyList();
        }
    }

    public static ResponseInfo<Object> sendOrder(long id, List<GameOrder> orders)  {
        String secret = DbUtil.getSecret();
        String json = null;
        try {
            json = HttpUtil.createPost(formatUrl(StrUtil.format(SEND_ORDER, id)))
                    .header("secret", secret)
                    .body(JsonUtil.mapper().writeValueAsString(orders))
                    .execute()
                    .body();
            LOG.info("secret {} sendOrder 返回结果 {}", secret, json);
            if (StrUtil.isBlank(json)) {
                return null;
            } else {
                return JsonUtil.mapper().readValue(json, new TypeReference<ResponseInfo<Object>>() {});
            }
        } catch (JsonProcessingException e) {
            LOG.warn("sendOrder 错误", e);
        }
        return null;
    }

    private static String formatUrl(String uri) {
        return HOST + ":" + PORT + BASE_PATH + uri;
    }

    public static void init(String host, int port) {
        ApiUtil.HOST = host;
        ApiUtil.PORT = port;
    }
}
